package com.example.crossborderecommerce.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.crossborderecommerce.entity.*;
import com.example.crossborderecommerce.service.IAdminService;
import com.example.crossborderecommerce.service.ISupplierService;
import com.example.crossborderecommerce.service.ISupplierService;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 供货商模块
 *
 * @author Yolanda
 * @since 2024-05-09
 */
@RestController
@Transactional
@RequestMapping("/supplier")
public class SupplierController {
    @Resource
    ISupplierService supplierService;
    @Resource
    IAdminService adminService;

    /**
     * 添加供应商
     * @param supplier
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody Supplier supplier) {
        boolean res = supplierService.save(supplier);
        if(res) {
            return Result.success("添加供应商成功！");
        }
        return Result.error("添加供应商失败！");
    }

    /**
     * 查询所有供应商
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll() {
        List<Supplier> supplierList = supplierService.list();
        if(supplierList != null) {
            return Result.success("查询所有供应商成功！", supplierList);
        }
        return Result.error("查询所有供应商失败！");
    }

    /**
     * 删除采购商
     * @param supplier
     * @return
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody Supplier supplier){
        boolean res = supplierService.removeById(supplier);
        if(res){
            return Result.success("删除采购订单成功！");
        }
        return Result.error("删除采购订单失败！");
    }


    /**
     * 删除一系列采购商
     * @param ids
     * @return
     */
    @GetMapping("/deleteBatch")
    public Result deleteBatch(@RequestParam String ids) {
        List<String> supplierIds = Arrays.stream(ids.split(",")).collect(Collectors.toList());
        boolean res = supplierService.removeByIds(supplierIds);
        if(res) {
            return Result.success("删除采购商成功！");
        }
        return Result.error("删除采购商失败！");
    }

    /**
     * 修改供应商
     * @param supplier
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody Supplier supplier) {
        boolean res = supplierService.saveOrUpdate(supplier);
        if (res) {
            return Result.success("修改供应商成功！");
        }
        return Result.error("修改供应商失败！");
    }

    /**
     * 分页查询所有的供应商
     * @param name
     * @param contactName
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/findPage")
    public Result findPage(@RequestParam(defaultValue = "") String name,
                           @RequestParam(defaultValue = "") String contactName,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<Supplier> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", name);
        queryWrapper.like("contact_name", contactName);
        Page<Supplier> supplierPage = supplierService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success("成功查询所有供应商信息！",supplierPage);
    }
    /**
     * 导出供应商信息
     * @param name
     * @param contactName
     * @param response
     * @return
     */
    @GetMapping("exportSupplierList")
    public Result exportSupplierList(@RequestParam(defaultValue = "") String name,
                                     @RequestParam(defaultValue = "") String contactName,
                                     HttpServletResponse response) {
        QueryWrapper<Supplier> queryWrapper = new QueryWrapper<>();
        List<Supplier> supplierList = supplierService.list(queryWrapper);
        queryWrapper.like("name", name);
        queryWrapper.like("contact_name", contactName);
        HSSFWorkbook wb = new HSSFWorkbook(); // 导出到excel文件中
        HSSFSheet sheet = wb.createSheet("表一");
        HSSFRow row = sheet.createRow(0);
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        HSSFCell cell = row.createCell((short) 0);
        cell.setCellValue("ID");
        cell.setCellStyle(style);
        cell = row.createCell((short) 1);
        cell.setCellValue("供应商名称");
        cell.setCellStyle(style);
        cell = row.createCell((short) 2);
        cell.setCellValue("联系人");
        cell.setCellStyle(style);
        cell = row.createCell((short) 3);
        cell.setCellValue("联系电话");
        cell.setCellStyle(style);
        cell = row.createCell((short)4);
        cell.setCellValue("地址");
        cell.setCellStyle(style);
        for (int i = 0; i < supplierList.size(); i++) {
            row = sheet.createRow(i + 1);
            Supplier supplier= supplierList.get(i);
            row.createCell((short) 0).setCellValue(supplier.getId());
            row.createCell((short) 1).setCellValue(supplier.getName());
            row.createCell((short) 2).setCellValue(supplier.getContactName());
            row.createCell((short) 3).setCellValue(supplier.getPhone());
            row.createCell((short) 4).setCellValue(supplier.getAddress());

        }
        String fileName = "exportSupplierList.xls";
        // 用户下载
        try {
            ServletOutputStream outputStream = null;
            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
            response.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode(fileName, "UTF-8"));
            outputStream = response.getOutputStream();
            wb.write(outputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return Result.success("exportProductBrandList");
    }
}
